//
//  PRFile.m
//  CandyJump
//
//  Created by Bon Lam on 03/03/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "PRDevice.h"
#import "PRFile.h"

//=====================================================================================================================
@implementation PRFile

//---------------------------------------------------------------------------------------------------------------------
static NSString *documentsDirectory = nil;

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getDocumentsDirectory {
	
	if (documentsDirectory != nil)  {return documentsDirectory;}
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return documentsDirectory;
}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getFilePath:(NSString *)filename {
	
	if (filename == nil)  {return nil;}
	
	//  Search documents directory, for any updates or downloaded content
	NSString *documentDirectory = [PRFile getDocumentsDirectory];
	NSString *documentPath = [documentDirectory stringByAppendingPathComponent:filename];
	if ([[NSFileManager defaultManager] isReadableFileAtPath:documentPath] == YES)
	{
		return documentPath;
	}
	
	//  Search App Main Bundle
	NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];
	NSString *bundlePath = [bundleDirectory stringByAppendingPathComponent:filename];
	if ([[NSFileManager defaultManager] isReadableFileAtPath:bundlePath] == YES)
	{
		return bundlePath;
	}
	
	//---------------------------------------------------------------------------------------------------------------------
	//  Get from resources directory for high defination if iPad, iPhone 4 and iPod Touch 4G
//	if ([PRDevice isHD] == YES) {
//		NSString *resourceDirectory = [bundleDirectory stringByAppendingPathComponent:PRFILE_RESOURCE_RETINA];
//		NSString *resourcePath = [resourceDirectory stringByAppendingPathComponent:filename];
//		if ([[NSFileManager defaultManager] isReadableFileAtPath:resourcePath] == YES)  {
//			return resourcePath;
//		}
//	}
	
//	//  Search Standard Resources folder
//	NSString *resourceDirectory = [bundleDirectory stringByAppendingPathComponent:PRFILE_RESOURCES];
//	NSString *resourcePath = [resourceDirectory stringByAppendingPathComponent:filename];
//	if ([[NSFileManager defaultManager] isReadableFileAtPath:resourcePath] == YES)
//	{
//		return resourcePath;
//	}
	
	//  File could not be found in all target directories
	NSLog(@"[PRFile getFilePath:filename] Error - file not found : \"%@\"", filename);
	return nil;
	
}

//---------------------------------------------------------------------------------------------------------------------
+ (NSString *)getDocumentsPath:(NSString *)filename {
	
	if (filename == nil)  {return nil;}
	
	NSString *fullPath = [[PRFile getDocumentsDirectory] stringByAppendingPathComponent:filename];
	return fullPath;
}


@end
